import 'package:flutter/material.dart';
import 'package:hkzf_flutter/utils/common_picker/index.dart';
import 'package:hkzf_flutter/widgets/common_form_item.dart';
// 表单select组件

class CommonSelectFormItem extends StatelessWidget {
  final String? label;//  表单标签
  final int? value;//  当前选中的选项索引
  final List<String>? options;//  选项列表
  final ValueChanged<int>? onChanged;//  选项改变回调函数

  const CommonSelectFormItem({super.key, this.label, this.options, this.value, this.onChanged});

  @override
  Widget build(BuildContext context) {
    return CommonFormItem(
      label: label,
      contentBuilder: (context){
        return GestureDetector(
          behavior: HitTestBehavior.translucent,// 控制手势事件穿透行为,允许事件继续传递给下层 widget
          onTap: (){
            var result = CommonPicker.showPicker(
              context: context, 
              options: options,
              value: value,
              height: MediaQuery.of(context).size.height * 0.33,
            );
            // 使用 then 方法监听 result 的异步结果,当用户完成选择时会触发回调
            result.then((selectedValue){
              print('选中的值是: $selectedValue');
              if(value != selectedValue && selectedValue != null && onChanged != null){
                onChanged!(selectedValue);
              }
            });
          },
          child: SizedBox(
            height: 40.0,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(options![value!],style: const TextStyle(fontSize: 16.0)),
                const Icon(Icons.keyboard_arrow_right,size: 30.0,),
              ],
            ),
          )
        );
      },
    );
  }
}
